#include "ConfigTcpConn.h"
#include "ConfigServer.h"


#include <iostream>
using namespace std;


namespace Config
{


	void ConfigServer::run()
	{
		Ice::CommunicatorPtr ic;
		try{
		Ice::InitializationData initData;
		initData.properties = Ice::createProperties();
		initData.properties->load("config.master");
		ic = Ice::initialize(initData);
		//ic = Ice::initialize(argc,argv);
		//ic->getProperties()->load("config.master");
		Ice::ObjectAdapterPtr adapter = ic->createObjectAdapter("Config.Master");
		ConfigMasterI * ptmp = new ConfigMasterI;
		//ConfigMasterPtr       masterptr = new ConfigMasterI;
		ConfigStore::getObj().setMaster(ptmp);
		
		ConfigMasterPtr masterptr = ptmp;
		ConfigStore::getObj().setMasterPtr(masterptr);
		//_g_MasterPtr = masterptr;
		adapter->add(masterptr, ic->stringToIdentity("ConfigMaster"));
		adapter->activate();
		ServerSocket svs(12345);
		TCPServer srv(new TCPServerConnectionFactoryImpl<ConfigTcpConnection>(), svs);
		srv.start();

		cout<<svs.address().port()<<endl;
		ic->waitForShutdown();
		}
		catch (const Ice::Exception& ex)
		{
			cerr<<"catch exception "<<ex.what()<<endl;
			getchar();
		}
	}


	ConfigStore & ConfigStore::getObj()
	{
		static ConfigStore instance;
		return instance;
	}

} // namespace Config